package me.lc.linklink.sprite;

import me.lc.linklink.core.GameManager;
import me.lc.linklink.core.GameMap;
import me.lc.linklink.core.GameSystem;
import me.lc.linklink.core.GameType;
import me.lc.linklink.core.ShopManager;
import me.lc.linklink.scene.GameScene;
import me.lc.linklink.util.AudioHelper;

import com.wiyun.engine.nodes.Label;
import com.wiyun.engine.nodes.Node;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.types.WYColor3B;
import com.wiyun.engine.types.WYPoint;
import com.wiyun.engine.utils.ResolutionIndependent;

public class ScoreToolsBlock extends Node {
	Sprite sptBG;
	Label finderCount;
	Label reArrangeCount;
	Label timerCount;

	float shopStartX = 1;
	float shopEndX = 58;

	float findStartX = 91;
	float findEndX = 142;

	float arrangeStartX = 225;
	float arrangeEndX = 273;

	float clockStartX = 351;
	float clockEndX = 398;

	static int FontSize = 40;

	public ScoreToolsBlock() {
		this.setPosition(0, 0);
		this.setAnchorPercent(0, 0);
		sptBG = Sprite.make(GameSystem.buttomBarTex);
		sptBG.setAnchorPercent(0, 0);
		sptBG.setPosition(0, 0);
		addChild(sptBG);

		// float y = ResolutionIndependent.resolveDp(GameSystem.BLOCK_SIZE) *
		// 0.125f;
		float y = 0;

		finderCount = Label
				.make(ShopManager.instance.assertInfo.getFinderCount() + "",
						FontSize);
		finderCount.setAnchorPercent(0, 0);
		finderCount.setColor(WYColor3B.make(255, 255, 255));
		float finderX = GameSystem.Screen_Width * 0.334375f;
		finderCount.setPosition(finderX, y);
		addChild(finderCount);

		reArrangeCount = Label.make(
				ShopManager.instance.assertInfo.getReArrangeCount() + "",
				FontSize);
		reArrangeCount.setAnchorPercent(0, 0);
		reArrangeCount.setColor(WYColor3B.make(255, 255, 255));
		float reArrangeCountX = GameSystem.Screen_Width * 0.60625f;
		reArrangeCount.setPosition(reArrangeCountX, y);
		addChild(reArrangeCount);

		timerCount = Label.make(ShopManager.instance.assertInfo.getTimerCount()
				+ "", FontSize);
		timerCount.setAnchorPercent(0, 0);
		timerCount.setColor(WYColor3B.make(255, 255, 255));
		float timerCountX = GameSystem.Screen_Width * 0.86875f;
		timerCount.setPosition(timerCountX, y);
		addChild(timerCount);
	}

	public void processTouch(WYPoint point) {
		if (GameManager.instance.isPaused()) {
			return;
		}
		if (point.x >= shopStartX && point.x <= shopEndX) {
			AudioHelper.clickItem();
			showShop();
		} else if (point.x >= findStartX && point.x <= findEndX) {
			// find
			AudioHelper.clickItem();
			if (ShopManager.instance.assertInfo.getFinderCount() > 0) {
				if (GameMap.findMatchBlocks(true)) {
					// 减少finder数量
					ShopManager.instance.assertInfo
							.setFinderCount(ShopManager.instance.assertInfo
									.getFinderCount() - 1);
					// 更新界面
					finderCount.setText(ShopManager.instance.assertInfo
							.getFinderCount() + "");
					ShopManager.instance.saveMyAssert();
				}
			} else {
				showShop();
			}
		} else if (point.x >= arrangeStartX && point.x <= arrangeEndX) {
			// arrange
			AudioHelper.clickItem();
			if (ShopManager.instance.assertInfo.getReArrangeCount() > 0) {
				// 减少arrange数量
				ShopManager.instance.assertInfo
						.setReArrangeCount(ShopManager.instance.assertInfo
								.getReArrangeCount() - 1);
				GameMap.reArrange();
				// 更新界面
				reArrangeCount.setText(ShopManager.instance.assertInfo
						.getReArrangeCount() + "");
				ShopManager.instance.saveMyAssert();
			} else {
				showShop();
			}
		} else if (point.x >= clockStartX && point.x <= clockEndX) {
			// clock
			AudioHelper.clickItem();
			if (GameManager.instance.CurrentGameType != GameType.Time) {
				if (ShopManager.instance.assertInfo.getTimerCount() > 0) {
					// 减少clock数量
					ShopManager.instance.assertInfo
							.setTimerCount(ShopManager.instance.assertInfo
									.getTimerCount() - 1);
					GameManager.instance.addTime();
					// 更新界面
					timerCount.setText(ShopManager.instance.assertInfo
							.getTimerCount() + "");
					ShopManager.instance.saveMyAssert();
				} else {
					showShop();
				}
			}
		}
	}

	public void updateToolsCount() {
		finderCount.setText(ShopManager.instance.assertInfo.getFinderCount()
				+ "");
		reArrangeCount.setText(ShopManager.instance.assertInfo
				.getReArrangeCount() + "");
		timerCount
				.setText(ShopManager.instance.assertInfo.getTimerCount() + "");
	}

	private void showShop() {
		GameScene.make().pauseGame();
	}
}
